package visao;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.text.MaskFormatter;

import modelo.Cd;
import modelo.Musico;
import controle.ControleCd;
import javax.swing.JRadioButton;
import javax.swing.JFormattedTextField;

public class TelaCd extends JFrame {

	private JPanel contentPane;
	private JTextField txtTitulo;
	private Cd cd = new Cd();
	private JComboBox cboxMusico;
	private ControleCd controle;
	private boolean ehDuplo;
	private boolean ehColetania;
	private JRadioButton rdbtnSimDuplo;
	private JRadioButton rdbtnNaoDuplo;
	private JRadioButton rdbtnSimColetania;
	private JRadioButton rdbtnNaoColetania;
	private JFormattedTextField txtAno;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TelaCd frame = new TelaCd();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TelaCd() {
		
		controle = new ControleCd();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblCd = new JLabel("CD");
		lblCd.setFont(new Font("Times New Roman", Font.BOLD, 20));
		lblCd.setBounds(197, 11, 46, 14);
		contentPane.add(lblCd);
		
		JLabel lblEhDuplo = new JLabel("Eh Duplo:");
		lblEhDuplo.setBounds(10, 61, 46, 14);
		contentPane.add(lblEhDuplo);
		
		JLabel lblTitulo = new JLabel("Titulo:");
		lblTitulo.setBounds(10, 93, 46, 14);
		contentPane.add(lblTitulo);
		
		JLabel lblAno = new JLabel("Ano:");
		lblAno.setBounds(10, 127, 46, 14);
		contentPane.add(lblAno);
		
		JLabel lblEhColetania = new JLabel("Eh Coletania:");
		lblEhColetania.setBounds(10, 155, 64, 14);
		contentPane.add(lblEhColetania);
		
		JLabel lblMusico = new JLabel("Musico:");
		lblMusico.setBounds(10, 185, 46, 14);
		contentPane.add(lblMusico);
		
		txtTitulo = new JTextField();
		txtTitulo.setColumns(10);
		txtTitulo.setBounds(84, 90, 86, 20);
		contentPane.add(txtTitulo);
		
		JButton btnSalvar = new JButton("Salvar");
		btnSalvar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			cd.setEhColetania(ehColetania);
			cd.setEhDuplo(ehDuplo);
			cd.setTitulo(txtTitulo.getText());
			cd.setAno(Integer.valueOf(txtAno.getText()));
			
			cd.setMusico((Musico)cboxMusico.getSelectedItem());
			
			try {
				controle.Salvar(cd);
			} catch (Exception e1) {
				JOptionPane.showMessageDialog(null, e1.getMessage());
				e1.printStackTrace();
			}
			rdbtnNaoColetania.setSelected(false);
			rdbtnNaoDuplo.setSelected(false);
			rdbtnSimColetania.setSelected(false);
			rdbtnSimDuplo.setSelected(false);
			txtAno.setText(null);
			txtTitulo.setText(null);
			}
		});
		btnSalvar.setBounds(305, 57, 91, 23);
		contentPane.add(btnSalvar);
		
		cboxMusico = new JComboBox();
		for (Musico m : controle.carregarMusicos()) {
			cboxMusico.addItem(m);
		}
		cboxMusico.setBounds(84, 181, 348, 22);
		contentPane.add(cboxMusico);
		
		List<Musico> musicos = controle.carregarMusicos();
		for (Musico m : musicos) {
			cboxMusico.addItem(m);
		}
		
		JButton btnBuscar = new JButton("Buscar");
		btnBuscar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//cliente.setCodigo(Integer.valueOf(txtIdCliente_Busca.getText());
			}
		});
		btnBuscar.setBounds(305, 89, 91, 23);
		contentPane.add(btnBuscar);
		
		rdbtnSimDuplo = new JRadioButton("Sim");
		rdbtnSimDuplo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(rdbtnSimDuplo.isSelected()){
					rdbtnNaoDuplo.setSelected(false);
					ehDuplo = true;
				}
			}
		});
		rdbtnSimDuplo.setBounds(82, 57, 52, 23);
		contentPane.add(rdbtnSimDuplo);
		
		rdbtnNaoDuplo = new JRadioButton("N\u00E3o");
		rdbtnNaoDuplo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(rdbtnNaoDuplo.isSelected()){
					rdbtnSimDuplo.setSelected(false);
					ehDuplo = false;
				}
			}
		});
		rdbtnNaoDuplo.setBounds(136, 57, 64, 23);
		contentPane.add(rdbtnNaoDuplo);
		
		rdbtnSimColetania = new JRadioButton("Sim");
		rdbtnSimColetania.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(rdbtnSimColetania.isSelected()){
					rdbtnNaoColetania.setSelected(false);
					ehColetania = true;
				}
			}
		});
		rdbtnSimColetania.setBounds(80, 151, 54, 23);
		contentPane.add(rdbtnSimColetania);
		
		rdbtnNaoColetania = new JRadioButton("N\u00E3o");
		rdbtnNaoColetania.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(rdbtnNaoColetania.isSelected()){
					rdbtnSimColetania.setSelected(false);
					ehColetania = false;
				}
			}
		});
		rdbtnNaoColetania.setBounds(134, 151, 52, 23);
		contentPane.add(rdbtnNaoColetania);
		
		try {
			txtAno = new JFormattedTextField(new MaskFormatter("####"));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		txtAno.setBounds(84, 124, 86, 17);
		contentPane.add(txtAno);
	}
}
